package util;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.Map;

public class MerchantGrade {
private static int preOrdersCount = 100;
	
	public static double clculateMerchantGrade(double preMgrade, Map<Date, Integer> ordersGrades, int curGrade, Date curOrderTime) {
		DecimalFormat df = new DecimalFormat("######0.0");
		double curMgrade;
		int ordersCount = 0;
		int gradesTotal = 0;
		double average = 0;
		int orderGrade = 0;
		
		for(Date date : ordersGrades.keySet()) {
			if ((curOrderTime.getTime() - date.getTime()) <= 1800000) {
				orderGrade = ordersGrades.get(date);
				if (orderGrade == -1) {
					orderGrade = 3;
				}
				ordersCount++;
				gradesTotal += orderGrade;
			}
		}
		average = ((double) gradesTotal / ordersCount);
		curMgrade = ((double)ordersCount / (double)(ordersCount + preOrdersCount)) * average + (preOrdersCount / ((double) ordersCount + preOrdersCount)) * preMgrade;
		preOrdersCount += ordersCount;
		
		return Double.parseDouble(df.format(curMgrade));
	}
}
